import store from '@/store'
// /**
//  * 判断用户是否拥有操作权限
//  * 根据传入的权限标识，查看是否存在用户权限标识集合
//  * @param level
//  */
export function hasPermission(level) {
    let hasPermission = false
    let permissions = store.state.UserLevel.perms
    for (let i = 0, len = permissions.length; i < len; i++) {
        if (permissions[i] === level) {
            hasPermission = true;
            break
        }
    }
    return hasPermission
}


/**
 * @name 时间戳转正式时间
 * @param {*} shijianchuo 
 * @returns 
 */
export function formatDate(shijianchuo) {
    function add0(m) {
        return m < 10 ? "0" + m : m;
    }
    //shijianchuo是整数，否则要parseInt转换
    var time = new Date(shijianchuo);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    // return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
    return (
        y + "-" + add0(m) + "-" + add0(d) + " " + add0(h) + ":" + add0(mm) + ":" + add0(s)
    );
}


//字符串转base64
export function encode(str) {
    // 对字符串进行编码
    var encode = encodeURI(str);
    // 对编码的字符串转化base64
    var base64 = btoa(encode);
    return base64;
}

// base64转字符串
export function decode(base64) {
    // 对base64转编码
    var decode = atob(base64);
    // 编码转字符串
    var str = decodeURI(decode);
    return str;
}


export function userLevel(level){
      let user = {
          1:'区信息员',
          2:'镇信息员',
          3:'村采集员'
      }
      return user[level];
}


export function IEVersion () {
    var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
    var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器
    var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器
    var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
    if (isIE) {
        var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
        reIE.test(userAgent);
        var fIEVersion = parseFloat(RegExp["$1"]);
        if (fIEVersion == 7) {
            return 7;
        }
        else if (fIEVersion == 8) {
            return 8;
        }
        else if (fIEVersion == 9) {
            return 9;
        }
        else if (fIEVersion == 10) {
            return 10;
        }
        else {
            return 6; //IE版本<=7
        }
    }
    else if (isEdge) {
        return 'edge'; //edge
    }
    else if (isIE11) {
        return 11; //IE11
    }
    else {
        return -1; //不是ie浏览器
    }
}


export function convertKey(arr,keyMap){
    let tempString = JSON.stringify(arr);
    for(var key in keyMap){
        var reg = `/"${key}":/g`;
        tempString = tempString.replace(eval(reg),'"'+keyMap[key]+'":');
    }
    return JSON.parse(tempString);
}


/**删除 无效的childreb
 * @param {Object} data
 */
 export function deleteChildren(data) {
    for (var i = 0; i < data.length; i++) {
        if (data[i].children) {
            if(data[i].children.length===0){
                delete data[i].children
            }else{
                // newArr.push(data[i].children)
                deleteChildren(data[i].children);
            }
            //判断children下是否还有数据，避免进入死循环
        }
    }
    return data;
}



export function trimArray(arr){
    let text = '';
    text = JSON.stringify(arr).substring(1)
    text = text.substring(0,text.length-1)
    return text
}

/**
 *  //构建 areas 回显数据
 * @param {*} json 
 * @returns 
 */
export function editDisplay(json) {
    var areas = new Array();
    for (var i = 0; i < json.length; i++) {
      if (json[i].administrativeHigh != "440605") {
      var arr = new Array();
      arr.push(json[i].administrativeHigh);
      arr.push(json[i].administrativeCode);
      areas.push(arr);
      }
    }
    return areas;
  }
  

  /**
   * 
   * @param {*} list 常量数组
   * @param {*} upArray 空数组
   * @param {*} dataSource 传入的数据
   */
  export function  makeData(list, upArray, dataSource) {
    dataSource.forEach((item) => {
      const array = [...upArray, item.id]
      list.push(array)
      if (item.children) {
          arguments.callee(list, array, item.children)
      }
    })
    return list
  }


//   export function  changecollectTaskTown(e) {
//     let newSelect = new Map();
//     // ,newSelectTown = [],children;
//     e.forEach((item) => {
//       let town;
//       let townIndex = this.townMatch?.indexOf(item[0]);
//       let villageIndex = this.villageMatch[townIndex]?.indexOf(item[1]);
//       town = JSON.parse(JSON.stringify(this.options[townIndex]));
//       town.children = [
//         JSON.parse(
//           JSON.stringify(this.villageMatch[townIndex][villageIndex])
//         ),
//       ];

//       if (newSelect.get(town.value)) {
//         newSelect.get(town.value).children = [
//           ...newSelect.get(town.value).children,
//           ...town.children,
//         ];
//       } else {
//         newSelect.set(town.value, town);
//       }
//     });
//     newSelect.forEach((item, key) => {
//       // newSelectTown.push(item);
//       this.townVillage[key] = item.children;
//     });
//     console.log(JSON.stringify(this.townVillage));
//     // this.townVillage = newSelectTown;
//   }